home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12415 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  141 lines

  1. Path: news.itsyd.bhp.com.au!news
  2. From: Richard Bowen <bowen.richard.rw@bhp.com.au>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Operator overload problem!
  5. Date: Wed, 20 Mar 1996 09:50:42 +1000
  6. Organization: BHP IT
  7. Message-ID: <314F4852.3A75@bhp.com.au>
  8. References: <DoIn1z.Gou@latcs1.lat.oz.au>
  9. NNTP-Posting-Host: 134.18.154.118
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. The compiler doesn't know that ostream is a class.  You need the
  16. include 
  17.  
  18. #include "iostream.h"
  19.  
  20. At the top of the file.
  21.  
  22.  
  23. Richard
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. Gregary J Boyles wrote:
  31. > I am trying to overload the << but I get the syntax errors : friends must be functions or
  32. > classes, 'ostream' cannot start a parameter declaration, 'Address::operator <<(int &,Address &)'
  33. > must be declared at the first set of astericks and declaration syntax error at the second set.
  34. > What the #$%& is it on about? I copied the operator overload function directly out of a program
  35. > which compiles and runs so why all of a sudden won't the bloody compiler accept it?
  36. > #include "defines.h"
  37. > class Address
  38. > {
  39. >      private : int Number;
  40. >                char *Street;
  41. >                char *City;
  42. >                int Zip;
  43. >      public : // Constructors
  44. >               Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
  45. >               Address();
  46. >               // Copy constructor
  47. >               Address(Address& AnAddress);
  48. >               // Deconstructor
  49. >               ~Address();
  50. >               // Operator overloads
  51. >             **************************************************************************
  52. >             *                                                                        *
  53. >             * friend ostream& operator <<(ostream& OutPutStream,Address& AnAddress); *
  54. >             *                                                                        *
  55. >             **************************************************************************
  56. >               // Functions
  57. >               void Change(int ANumber,const char *AStreet,const char *ACity,int AZip);
  58. > };
  59. > #endif
  60. > // address.cpp
  61. > #include "address.h"
  62. > #include <string.h>
  63. > // Constructors
  64. > Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
  65. > {
  66. >      Number=ANumber;
  67. >      Street=new char[strlen(AStreet)+1];
  68. >      strcpy(Street,AStreet);
  69. >      City=new char[strlen(ACity)+1];
  70. >      strcpy(City,ACity);
  71. >      Zip=AZip;
  72. > }
  73. > Address::Address()
  74. > {
  75. >      Number=0;
  76. >      Street=new char[strlen("")+1];
  77. >      strcpy(Street,"");
  78. >      City=new char[strlen("")+1];
  79. >      strcpy(City,"");
  80. >      Zip=0;
  81. > }
  82. > // Copy constructor
  83. > Address::Address(Address& AnAddress)
  84. > {
  85. >      Number=AnAddress.Number;
  86. >      Street=new char[strlen(AnAddress.Street)+1];
  87. >      strcpy(Street,AnAddress.Street);
  88. >      City=new char[strlen(AnAddress.City)+1];
  89. >      strcpy(City,AnAddress.City);
  90. >      Zip=AnAddress.Zip;
  91. > }
  92. > // Deconstructor
  93. > Address::~Address()
  94. > {
  95. >      Number=0;
  96. >      strcpy(Street,"");
  97. >      strcpy(City,"");
  98. >      Zip=0;
  99. > }
  100. > // Operator overloads
  101. > ***************************************************************
  102. >                                                               *
  103. > ostream& operator <<(ostream& OutPutStream,Address& AnAddress)*
  104. >                                                               *
  105. > ***************************************************************
  106. > {
  107. >      OutPutStream<<"Street : "<<AnAddress.Number<<" "<<AnAddress.Street<<EOLN;
  108. >      OutPutStream<<"City : "<<AnAddress.City<<EOLN;
  109. >      OutPutStream<<"Zip code : "<<AnAddress.Zip<<EOLN;
  110. > }
  111. > // Functions
  112. > void Address::Change(int ANumber,const char *AStreet,const char *ACity,int AZip)
  113. > {
  114. >      Number=ANumber;
  115. >      delete Street;
  116. >      Street=new char[strlen(AStreet)+1];
  117. >      strcpy(Street,AStreet);
  118. >      delete City;
  119. >      City=new char[strlen(ACity)+1];
  120. >      strcpy(City,ACity);
  121. >      Zip=AZip;
  122. > }
  123.